<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/19
 * Time: 14:42
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{
    public function login(Request $request)
    {

        $username = $request->input('username');
        $password = $request->input('password');
        $captcha = $request->input('code');
        $isAdmin = DB::select('select * from admin where username = ?', [$username]);
        $data = [];
        $res = captcha_check($captcha);
        if ($res) {
            if ($isAdmin) {
                $admin = $isAdmin[0]->password;
                if ($admin == md5($password)) {
                    $data['code'] = 1;
                    $data['msg'] = '登录成功';

                } else {
                    $data['code'] = 2;
                    $data['msg'] = '密码错误';

                }
            } else {
                $data['code'] = 0;
                $data['msg'] = '用户不存在';

            }
        } else {
            $data['code'] = 3;
            $data['msg'] = '验证码错误';

        }
        return $data;
    }

    public function getCode()
    {
        $url = captcha_src();
        return $url;
    }
}